package com.nclg.exam.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class LogAspect {

    private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution( public * com.nclg.exam.controller.*.*.*(..))")
    public void log(){}

    @Before("log()")
    public void  doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //url
        logger.info("请求地址:"+"url={}" , request.getRequestURL());

        //method
        logger.info("请求方法:"+"method={}" ,request.getMethod());

        //ip,也可以获取端口号
        logger.info("IP地址:"+"ip={}" , request.getRemoteAddr());

        //获取端口号
        logger.info("请求端口:"+"host={}"+request.getRemoteHost());

        //类方法
        logger.info("请求方法:"+"class_method={}" , joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());

        //方法的参数
        logger.info("方法参数:"+"args={}" , joinPoint.getArgs());

    }

}
